﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace HPPClientLibrary.XmlableObject
{
    public class FileInfomation:IXmlable
    {
        private UserList _userList;
        private File _file;
        public FileInfomation(UserList userList,File file)
        {
            _userList = userList;
            _file = file;
        }

        public virtual XDocument ToXml()
        {
            XDocument xdoc = new XDocument();
            xdoc.Declaration = new XDeclaration("1.0", "utf-8", "yes");
            XElement fileInfomation = new XElement("FileInfomation");
            XElement userList = new XElement("UserList");
            if (_userList == null || _userList.Count() == 0 || _file == null)
            {
                fileInfomation.Add(userList);
                xdoc.Add(fileInfomation);
                return xdoc;
            }
            foreach (User user in _userList)
            {
                userList.Add(new XElement("User", new XAttribute("IP", user.IP), new XAttribute("Port", user.Port)));
            }
            fileInfomation.Add(userList);
            fileInfomation.Add(new XElement("FileSize", new XAttribute("Size", _file.Size)), new XElement("Blocks", new XAttribute("Number", _file.BlockCount)), new XElement("LastBlock", new XAttribute("Size", _file.LastBlockSize)));
            xdoc.Add(fileInfomation);
            return xdoc;
        }
    }
}
